package com.interagriculture.service.impl;

import com.interagriculture.common.Constants;
import com.interagriculture.entity.StudioInfo;
import com.interagriculture.mapper.StudioInfoMapper;
import com.interagriculture.service.StudioInfoService;
import com.interagriculture.utils.UploadFileUtil;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * (StudioInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-10-24 19:13:18
 */
@Service("studioInfoService")
public class StudioInfoServiceImpl implements StudioInfoService {
    @Resource
    private StudioInfoMapper studioInfoMapper;
    @Resource
    private UploadFileUtil uploadFileUtil;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public StudioInfo queryById(Integer id) {
        return this.studioInfoMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param studioInfo 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<StudioInfo> queryByPage(StudioInfo studioInfo, PageRequest pageRequest) {
        long total = this.studioInfoMapper.count(studioInfo);
        return new PageImpl<>(this.studioInfoMapper.queryAllByLimit(studioInfo, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param studioInfo 实例对象
     * @return 实例对象
     */
    @Override
    public StudioInfo insert(StudioInfo studioInfo) {
        this.studioInfoMapper.insert(studioInfo);
        return studioInfo;
    }

    /**
     * 修改数据
     *
     * @param studioInfo 实例对象
     * @return 实例对象
     */
    @Override
    public StudioInfo update(StudioInfo studioInfo) {
        this.studioInfoMapper.update(studioInfo);
        return this.queryById(studioInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public void deleteById(Integer id) {
        studioInfoMapper.deleteById(id);
    }

    /**
     * 无条件查询实验室基本信息
     * @return
     */
    @Override
    public List<StudioInfo> findEssInfo() {
        List<StudioInfo> studioInfoList = studioInfoMapper.selectEssInfo();
        return studioInfoList;
    }

    @Override
    public void saveOrUpdateInfo(StudioInfo studioInfo) {
        if(studioInfo.getId() == null){
            // 新增操作
            studioInfoMapper.insert(studioInfo);
        }else{
            // 修改操作
            studioInfoMapper.update(studioInfo);
        }

    }
}
